$(document).ready(function(){
	$('#grid').datagrid({
		url: baseUrl + '/manager/user/list/a/rows',
		sortName: 'id',
		sortOrder: 'desc',
		remoteSort: false,
		nowrap: false,
		striped: true,
		idField: 'id',
		frozenColumns: [[
		  {field: 'ck', checkbox: true},
		  {title:'编号',field:'id',width:40,sortable:true, align:'center'}
		]],
		pagination: true,
		rownumbers:true,
		toolbar: [{
			text: '新增用户',
			iconCls: 'add',
			handler: function() {
				window.location = baseUrl + '/manager/user/add';
			}
		},'-',{
			text: '修改用户',
			iconCls: 'edit',
			handler: edituser
		},'-',{
			text: '删除用户',
			iconCls: 'delete',
			handler: deleteuser
		}]
	});
	
	$('#search').submit(function(){
		$('#grid').datagrid('reload', {
			search: JSON.stringify({account: $('#search input[name=account]').val()})
		});
	});
	
	
	$('#add input[name=role_id]').combobox({
		url: baseUrl + '/manager/user/add/a/combo',
		valueField:'id',    
		textField:'role_name',
		panelHeight: 'auto'
	});
	
	$('#add').submit(function() {
		$.ajax({
			url: baseUrl + '/manager/user/add/a/submit',
			type: 'post',
			data: $('#add').serialize(),
			dataType: 'json',
			success: function(json) {
				
				if(!json.success) {
					$.messager.alert('警告', json.message, 'warning');
					return false;
				}
				
				window.location = baseUrl + '/manager/user/list';
				
			}
		});
	});
	
	
});

var edituser = function() {
	var selected = $('#grid').datagrid('getSelected');
	if(selected) {
		window.location = baseUrl + '/manager/user/edit/id/' + selected.id;
		return true;
	}
	
	$.messager.alert('警告', '请先选择一条数据', 'warning');
};

var deleteuser = function() {
	var ids = new Array();
	var rows = $('#grid').datagrid('getSelections');
	
	if(rows != null && rows != '') {
		for(var i = 0; i < rows.length; i ++) {
			ids.push(rows[i].id);
		}
		
		//ajax提交
		$.ajax({
			url: baseUrl + '/manager/user/delete',
			type: 'post',
			data: {ids: ids.join(',')},
			dataType: 'json',
			success: function(json) {
				
			}
		});
		
		return true;
	}
	
	$.messager.alert('警告', '至少请先选择一条数据', 'warning');
};